<?php

namespace App\Models\Entity\Column;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Album
 * @package App\Models\Entity\Column
 * @version August 1, 2017, 4:32 pm UTC
 */
class Album extends Model
{
    use SoftDeletes;

    public $table = 'albums';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'title',
        'subtitle',
        'short_desc',
        'long_desc',
        'backimg',
        'created_at',
        'updated_at',
        // 上下线, 发布
        'online_status',
        'moderate_status',
        'publish_status',
        'moderate_by',
        'moderate_at',
        'online_at',
        'offline_at',
        'user_id',
        'publish_down_status',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name'       => 'string',
        'title'      => 'string',
        'subtitle'   => 'string',
        'short_desc' => 'string',
        'long_desc'  => 'string',
        'backimg'    => 'string',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'name'       => 'required|max:100',
        'title'      => 'max:100',
        'subtitle'   => 'max:100',
        'short_desc' => 'required|max:100',
        'backimg'    => 'required|max:200',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     **/
    public function cols()
    {
        return $this->belongsToMany(\App\Models\Entity\Column\Column::class, 'album_columns', 'album_id', 'column_id');
    }

    /**
     * [vassetis description]
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
     */
    public function vassetis()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Video\Vasseti::class, 'album_vassetis', 'album_id', 'vasseti_id');
    }

    /**
    * 过滤上下线数据
    **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
    * 过滤上线发布数据
    **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
    * 过滤下线发布数据
    **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }
}
